//
//  ReportHistoryView.swift
//  skincare
//
//  Created by mac on 2023/5/20.
//

import SwiftUI


// 为了方便，我们选择使用Realm数据库ORM


// 创建一个Realm对象

struct ReportHistoryView: View {
    @State private var selectedDate: Date?
    var body: some View {
       
        ScrollView{
            VStack(spacing: 25){
                MyCalendarView(onDateSelected: { date in
                    // Set the selected date
                    self.selectedDate = date})
                    .frame(height: 200)
                
                if let date = selectedDate {
                                Text("选择的日期是: \(date,formatter: dateFormatter)")
                                    .padding()
                            } else {
                                Text("No date selected")
                                    .padding()
                            }
                        
                // 日历部分
//                Rectangle()
//                    .fill(.blue)
//                    .frame(height:200)
                // 测肤记录部分
                SingleHistoryView(Date: "2023-1-13", Score_All: 89, Acne: 0, Comedo: 0, Oil: 1)
                SingleHistoryView(Date: "2023-1-11", Score_All: 88, Acne: 1, Comedo: 0, Oil: 1)
                SingleHistoryView(Date: "2023-1-10", Score_All: 85, Acne: 0, Comedo: 1, Oil: 0)
                SingleHistoryView(Date: "2023-1-9", Score_All: 83, Acne: 1, Comedo: 0, Oil: 0)
            
              
            
        
            
            
            }
        }
    }
    private let dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
            return dateFormatter
        }()
}


struct SingleHistoryView: View {
    
    let Date: String
    let Score_All: Int
    let Acne: Int
    let Comedo: Int
    let Oil: Int
    
    let str1: String
    let str2: String
    let str3: String
    
    init(Date: String, Score_All: Int, Acne: Int, Comedo: Int, Oil: Int) {
        self.Date = Date
        self.Score_All = Score_All
        self.Acne = Acne
        self.Comedo = Comedo
        self.Oil = Oil
        str1 = Acne == 0 ? "轻度痘痘" : "重度痘痘"
        str2 = Comedo == 0 ? "轻度黑头" : "重度黑头"
        str3 = Oil == 0 ? "干皮" : "油皮"
    }
    
    var body: some View {
        
        VStack{
            RoundedRectangle(cornerRadius: 20)
                .fill(Color("systemBlue"))
                .frame(width:370, height:100)
                .overlay(
                    HStack{
                        ZStack{
                            Circle()
                                .trim(from: 0.35, to: 1.0)
                                .stroke(Color.white, style: StrokeStyle(lineWidth: 7, dash: [5]))
                                .frame(width: 85)
                                .rotationEffect(Angle(degrees: 207))
                            
                            VStack(spacing:5){
                                Text("总分")
                                    .font(.system(size: 15))
                                Text("\(Score_All)")
                                    .font(.system(size: 40))
                                    .fontWeight(.semibold)
                            }
                            .foregroundColor(Color.white)
                            .offset(y:-10)
                            
                        }
                        .padding(.horizontal, 10)
                        
                        
                        Path { path in
                            path.move(to: CGPoint(x: 0, y: 15))
                            path.addLine(to: CGPoint(x: 0, y: 90))
                        }
                        .stroke(style: StrokeStyle(lineWidth: 3, dash: [8, 3]))
                        .foregroundColor(Color.white)
                        .frame(width:5)
                        .padding(.horizontal, 5)
                        
                        
                        VStack(alignment: .leading){
                            Text(Date)
                                .font(.system(size: 20))
                                .foregroundColor(Color.white)
                                .fontWeight(.semibold)
                            Text("测肤报告")
                                .font(.system(size: 16))
                                .foregroundColor(Color.white)
                                .fontWeight(.semibold)
                            
                            HStack{
                                Text(str1)
                                    .font(.system(size: 10))
                                    .padding(2)
                                    .padding(.horizontal, 2)
                                    .background(
                                        Capsule()
                                            .fill(Color("gray").opacity(0.8))
                                    )
                                Text(str2)
                                    .font(.system(size: 10))
                                    .padding(2)
                                    .padding(.horizontal, 2)
                                    .background(
                                        Capsule()
                                            .fill(Color("gray").opacity(0.8))
                                    )
                                Text(str3)
                                    .font(.system(size: 10))
                                    .padding(2)
                                    .padding(.horizontal, 2)
                                    .background(
                                        Capsule()
                                            .fill(Color("gray").opacity(0.8))
                                    )
                                Text("中度黑眼圈")
                                    .font(.system(size: 10))
                                    .padding(2)
                                    .padding(.horizontal, 2)
                                    .background(
                                        Capsule()
                                            .fill(Color("gray").opacity(0.8))
                                    )
                            }
                        }
                        .overlay(
                            Image("catHead")
                                .resizable()
                                .scaledToFit()
                                .frame(width:250, height:130)
                                .offset(x:50, y:-30)
                        )
                        Spacer()
                    }
                )
        }
    }
}



struct ReportHistoryView_Previews: PreviewProvider {
    static var previews: some View {
        ReportHistoryView()
    }
}
